/**
ParticleEmitter.cpp
Title: Binge Particle Emitter Program File
Descr: ParticleEmitter Class

Copyright (c) 2010, Brant Ryan Anderson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

#include "../../binge/utilities/ParticleEmitter.hpp"

namespace binge
{

ParticleEmitter::ParticleEmitter()
{
	//initialize particles to defaults
	image = 0;
	max = 100;
	length = 100;
	direction = 0;
	alphaMin = 254;    alphaMax = 255;
	minR = 0; maxR = 255;
	minG = 0; maxG = 255;
	minB = 0; maxB = 255;
	spread = 10;
	velocity = 1.0f;
	scale = 1.0f;
}

bool
ParticleEmitter::loadImage(std::string imageFile)
{
	image = new Texture();
	return image->Load(imageFile);
}


ParticleEmitter::~ParticleEmitter()
{
	//destroy particles
	for (iter i = particles.begin(); i != particles.end(); ++i)
	{
		delete *i;
	}
	particles.clear();
	if (image != 0) {
	    delete image;
	    image = 0;
	}
}

void ParticleEmitter::add()
{
	static double PI_DIV_180 = 3.1415926535 / 180.0f;
	double vx,vy;

	//create a new particle
	Sprite *p = new Sprite();
	p->setImage(image);
	p->setPosition(position.getX(), position.getY());

	//add some randomness to the spread
	double variation = (rand() % spread - spread/2) / 100.0f;

	//set linear velocity
	double dir = direction - 90.0;
	vx = cos( dir * PI_DIV_180) + variation;
	vy = sin( dir * PI_DIV_180) + variation;
	p->setVelocity(vx * velocity,vy * velocity);

	//set the scale
	p->setScale(scale);

	//add particle to the emitter
	particles.push_back(p);

}

void ParticleEmitter::draw()
{
	//draw particles
	for (iter i = particles.begin(); i != particles.end(); ++i)
	{
		(*i)->draw();
	}
}

void ParticleEmitter::update()
{
	static Timer timer;

	//do we need to add a new particle?
	if ((int)particles.size() < max)
	{
		//trivial but necessary slowdown
		if (timer.Stopwatch(1)) add();
	}


	for (iter i = particles.begin(); i != particles.end(); ++i)
	{
		//update particle's position
		(*i)->move();

		//is particle beyond the emitter's range?
		if ( (*i)->getPosition().Distance(this->position) > length)
		{
			//reset particle to the origin
			(*i)->setX(position.getX());
			(*i)->setY(position.getY());
		}
	}
}

void ParticleEmitter::setAlphaRange(int min,int max)
{
	alphaMin=min;
	alphaMax=max;
}

void ParticleEmitter::setColorRange(int r1,int g1,int b1,int r2,int g2,int b2)
{
	minR = r1; maxR = r2;
	minG = g1; maxG = g2;
	minB = b1; maxB = b2;
}

} // namespace
